/**
 *
 */
// 涉及的表名
const dbName = require("../config.js");

var dao = {};
var util = {};
// 初始化
dao.init = function(obj) {
	util = obj;
}
/**
 * 查 - 根据id获取单条记录
 * @params {String} id
 * @params {Object} fieldJson 字段显示规则
 * 调用示例
 * let withdrawalWayInfo = await vk.daoCenter.withdrawalWayDao.findById(id);
 */
dao.findById = async (id, fieldJson) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.findById({
		dbName: dbName.withdrawalWay,
		id,
		fieldJson
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};
/**
 * 查 - 根据whereJson获取单条记录
 * @params {Object} whereJson 条件
 * @params {Object} fieldJson 字段显示规则
 * 调用示例
let withdrawalWayInfo = await vk.daoCenter.withdrawalWayDao.findByWhereJson({

});
 */
dao.findByWhereJson = async (whereJson, fieldJson) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.findByWhereJson({
		dbName: dbName.withdrawalWay,
		whereJson,
		fieldJson
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 增 - 添加一条记录
 * @params {Object} dataJson 添加的数据
 * 调用示例
await vk.daoCenter.withdrawalWayDao.add({

});
 */
dao.add = async (dataJson) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.add({
		dbName: dbName.withdrawalWay,
		dataJson
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 增 - 添加多条记录
 * @params {Object} dataJson 添加的数据
 * 调用示例
await vk.daoCenter.withdrawalWayDao.adds(dataArr);
 */
dao.adds = async (dataArr) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.adds({
		dbName: dbName.withdrawalWay,
		dataJson: dataArr
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 删 - 删除多条记录
 * @params {Object} whereJson 条件
 * 调用示例
await vk.daoCenter.withdrawalWayDao.del({

});
 */
dao.del = async (whereJson) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.del({
		dbName: dbName.withdrawalWay,
		whereJson
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 删 - 据ID删除单条数据
 * @params {String} id
 * 调用示例
await vk.daoCenter.withdrawalWayDao.deleteById(id);
 */
dao.deleteById = async (id) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.deleteById({
		dbName: dbName.withdrawalWay,
		id
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 改 - 批量修改
 * @params {Object} whereJson 条件
 * @params {Object} dataJson 修改的数据
 * 调用示例
await vk.daoCenter.withdrawalWayDao.update({
	whereJson:{

	},
	dataJson:{

	}
});
 */
dao.update = async (obj={}) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.update({
		...obj,
		dbName: dbName.withdrawalWay,
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 改 - 根据ID修改数据
 * @params {String} id
 * @params {Object} dataJson 修改的数据
 * 调用示例
await vk.daoCenter.withdrawalWayDao.updateById(id, dataJson);
 */
dao.updateById = async (id, dataJson) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.updateById({
		dbName: dbName.withdrawalWay,
		id,
		dataJson
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 改 - 更新并返回更新后的数据（无论条件匹配到多少条记录，只会修改第一条记录，同时返回修改后的数据）
 * @params {Object} whereJson 条件
 * @params {Object} dataJson 修改的数据
 * 调用示例
await vk.daoCenter.withdrawalWayDao.updateAndReturn({
	whereJson:{

	},
	dataJson:{

	}
});
 */
dao.updateAndReturn = async (obj={}) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.updateAndReturn({
		...obj,
		dbName: dbName.withdrawalWay
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 查 - 获取记录总条数
 * @params {Object} whereJson 条件
 * 调用示例
await vk.daoCenter.withdrawalWayDao.count(whereJson);
 */
dao.count = async (whereJson) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.count({
		dbName: dbName.withdrawalWay,
		whereJson
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};


/**
 * 查 - 获取数据列表
 * 调用示例
let withdrawalWayList = await vk.daoCenter.withdrawalWayDao.select({
	pageIndex:1,
	pageSize:20,
	getMain:false,
	whereJson:{

	},
	fieldJson:{},
	sortArr:[{ "name":"_id", "type":"desc" }],
});
 */
dao.select = async (obj={}) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.select({
		...obj,
		dbName: dbName.withdrawalWay
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 查 - 获取数据列表
 * 调用示例
let withdrawalWayList = await vk.daoCenter.withdrawalWayDao.selects({
	pageIndex:1,
	pageSize:20,
	getMain:false,
	whereJson:{

	},
	fieldJson:{},
	sortArr:[{ "name":"_id", "type":"desc" }],
	// 副表列表
	foreignDB:[
		{
			dbName:"副表表名",
			localKey:"主表外键名",
			foreignKey:"副表外键名",
			as:"副表as字段",
			limit:1
		}
	]
});
 */
dao.selects = async (obj={}) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.selects({
		...obj,
		dbName: dbName.withdrawalWay
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 查 - 获取数据列表
 * 调用示例
res = await vk.daoCenter.withdrawalWayDao.getTableData({ data });
 */
dao.getTableData = async (obj={}) => {
	let { vk, db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.baseDao.getTableData({
		...obj,
		dbName: dbName.withdrawalWay
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 修改用户提现方式为非默认
 * 调用示例
await vk.daoCenter.withdrawalWayDao.updateMywithdrawalWayDefault({
	id : _id,
	user_id : uid
});
 */
dao.updateMywithdrawalWayDefault = async (data) => {
	let { vk , db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	let {
		id = "___",
		user_id = "___"
	} = data;
	res = await vk.baseDao.update({
		dbName:dbName.withdrawalWay,
		whereJson:{
			_id : _.neq(id),
			user_id : user_id,
			is_default : true,
		},
		dataJson : {
			is_default : false
		}
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

/**
 * 修改用户提现方式为非默认
 * 调用示例
let wayInfo = await vk.daoCenter.withdrawalWayDao.getMywithdrawalWayDefault(uid);
 */
dao.getMywithdrawalWayDefault = async (uid="___") => {
	let { vk , db, _ } = util;
	let res = {};
	// 数据库操作开始-----------------------------------------------------------
	res = await vk.daoCenter.withdrawalWayDao.findByWhereJson({
		user_id: uid,
		is_default: true
	});
	// 数据库操作结束-----------------------------------------------------------
	return res;
};


/**
 * 增 - 添加一条记录
 * @params {Object} dataJson 添加的数据
 * 调用示例
await vk.daoCenter.withdrawalWayDao.editWithdrawalWay({

});
 */
dao.editWithdrawalWay = async (data) => {
	let { vk, db, _ } = util;
	let res = { code:0, msg:"" };
	// 数据库操作开始-----------------------------------------------------------
	let {
		_id,
		user_id,
		mode,
		type,
		bank_name,
		bank_open,
		account_no,
		name,
		mobile,
		comment,
		is_default = false
	} = data;
	if (vk.pubfn.isNull(user_id)) return { code: -1, msg: "user_id不能为空" };
	if (vk.pubfn.isNull(mode)) return { code: -1, msg: "mode不能为空" };
	if (mode === "update") {
		if (vk.pubfn.isNull(_id)) return { code: -1, msg: "_id不能为空" };
	}

	let dataJson = {
		type,
		bank_name,
		bank_open,
		account_no,
		name,
		mobile,
		comment,
		is_default,
	};

	if (vk.pubfn.isNull(_id)) {
		// 添加
		// 添加额外字段
		dataJson = vk.pubfn.objectAssign(dataJson, {
			user_id,
			verified: false
		});
		_id = await vk.daoCenter.withdrawalWayDao.add(dataJson);
		// 返回添加后的数据
		res.info = await vk.daoCenter.withdrawalWayDao.findById(_id);
		res.msg = "添加成功";
	} else {
		// 修改并返回修改后的数据
		res.info = await vk.daoCenter.withdrawalWayDao.updateAndReturn({
			whereJson: {
				_id,
				user_id
			},
			dataJson
		});
		res.msg = "修改成功";
	}

	if (is_default) {
		// 将其他的提现方式设为非默认
		await vk.daoCenter.withdrawalWayDao.updateMywithdrawalWayDefault({
			id: _id,
			user_id,
		});
	}
	// 数据库操作结束-----------------------------------------------------------
	return res;
};

module.exports = dao;
